/**
 * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
 *
 * This library is free software; you can redistribute it and/or modify it under
 * the terms of the GNU Lesser General Public License as published by the Free
 * Software Foundation; either version 2.1 of the License, or (at your option)
 * any later version.
 *
 * This library is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
 * details.
 */

package org.openwork.portlet.skillmanagement.service.impl;

import java.util.Date;
import java.util.List;

import org.openwork.portlet.skillmanagement.DuplicateSkillUserException;
import org.openwork.portlet.skillmanagement.NoSuchSkillUserException;
import org.openwork.portlet.skillmanagement.model.SkillUser;
import org.openwork.portlet.skillmanagement.service.base.SkillUserLocalServiceBaseImpl;
import org.openwork.portlet.skillmanagement.service.persistence.SkillUserPK;

import com.liferay.portal.kernel.exception.PortalException;
import com.liferay.portal.kernel.exception.SystemException;
import com.liferay.portal.kernel.util.OrderByComparator;
import com.liferay.portal.model.User;

/**
 * <a href="SkillUserLocalServiceImpl.java.html"><b><i>View Source</i></b></a>
 *
 * @author Brian Wing Shun Chan
 */
public class SkillUserLocalServiceImpl extends SkillUserLocalServiceBaseImpl {
	
	public SkillUser addSkillUser(long auditUserId, long userId, long skillId, short experience) throws PortalException, SystemException {
		SkillUser skillUser = createSkillUser(new SkillUserPK(userId, skillId));
		
		skillUser.setExperience(experience);
		skillUser.setAuditUserId(auditUserId);
		skillUser.setCreateDate(new Date());
		
		validateCreate(auditUserId, userId, skillId, experience);
		
		skillUser = updateSkillUser(skillUser);
		
		return skillUser;
	}
	
	protected void validateCreate(long auditUserId, long userId, long skillId, short experience) throws PortalException, SystemException {
		try {
			getSkillUser(new SkillUserPK(userId, skillId));
			
			throw new DuplicateSkillUserException();
		} catch(NoSuchSkillUserException ex) { }
		
		
	}
	
	public SkillUser updateSkillUser(long auditUserId, long userId, long skillId, short experience) throws PortalException, SystemException {
		SkillUser skillUser = getSkillUser(new SkillUserPK(userId, skillId));
		
		skillUser.setExperience(experience);
		skillUser.setAuditUserId(auditUserId);
		skillUser.setCreateDate(new Date());
		
		skillUser = updateSkillUser(skillUser);
		
		return skillUser;
	}
	
	public List<User> searchBySkillIds(long[] ids) throws PortalException, SystemException {
		return null;
	}
	
	public int searchCountBySkillIds(long[] ids) throws PortalException, SystemException {
		return 0; 
	}
	
	public int searchCountByUserId(long userId)  throws PortalException, SystemException {
		return skillUserPersistence.countByUserId(userId);
	}
	public List<SkillUser> searchByUserId(long userId, int start, int end, OrderByComparator orderByComparator)  throws PortalException, SystemException {
		return skillUserPersistence.findByUserId(userId, start, end, orderByComparator);
	}
	
	public List<SkillUser> searchByUserId(long userId)  throws PortalException, SystemException {
		return skillUserPersistence.findByUserId(userId);
	}
	public int searchCountBySkillId(long skillId)  throws PortalException, SystemException {
		return skillUserPersistence.countBySkillId(skillId);
	}
	
	public List<SkillUser> searchBySkillId(long skillId)  throws PortalException, SystemException {
		return skillUserPersistence.findBySkillId(skillId);
	}
	public List<SkillUser> searchBySkillId(long skillId, int start, int end, OrderByComparator orderByComparator)  throws PortalException, SystemException {
		return skillUserPersistence.findBySkillId(skillId,start, end, orderByComparator);
	}
	
	public int searchCountBySkillId(long skillId, int value) throws PortalException, SystemException {
		return 0;
	}
	public List<SkillUser> searchBySkillId(long skillId, int value, int start, 
							int end, OrderByComparator orderByComparator) throws PortalException, SystemException {
		return null;
	}
}